<template>
  <div id="LessonDetail">
    <a-form layout="inline" style="margin-bottom: 8px">
      <a-form-item label="" class="searchItem">
        <span style="color: white"> {{ detailData.courseName }}</span>
      </a-form-item>
      <a-form-item label="">
        <a-button type="primary" @click="addbtn"> 添加课程计划资源 </a-button>
        <a-button type="primary" style="margin-left: 10px" @click="backup">
          返回
        </a-button>
      </a-form-item>
    </a-form>
    <a-table
      bordered
      :scroll="{ y: 500 }"
      :columns="columns"
      style="margin-top: 16px"
      :data-source="tableData"
      :pagination="tablePagination"
      :rowKey="(record, index) => index"
    >
      <template slot="name" slot-scope="text, record">
        <a-tooltip placement="top" v-if="record.name">
          <template slot="title">
            <span>{{ record.name }}</span>
          </template>
          <span style="cursor: default">{{ record.name }}</span>
        </a-tooltip>
        <span v-else-if="!record.name">—</span>
      </template>
      <template slot="tapdProjectId" slot-scope="text, record">
        <a-tooltip placement="top" v-if="record.tapdProjectId">
          <template slot="title">
            <p>{{ record.tapdProjectId }}</p>
          </template>
          <span style="cursor: default">{{ record.tapdProjectId }}</span>
        </a-tooltip>
        <span v-else-if="!record.tapdProjectId">—</span>
      </template>
      <template slot="operation" slot-scope="text, record">
        <a-button type="primary" @click="editClick(record)">查看</a-button>
        <a-button
          type="danger"
          @click="deleteClick(record)"
          style="margin-left: 10px"
          >删除</a-button
        >
      </template>
    </a-table>
    <a-modal v-model="visible" :title="title" @cancel="handleCancel">
      <a-form-model
        :label-col="{ span: 5 }"
        :wrapper-col="{ span: 12 }"
        ref="formRef"
        :model="form"
        :rules="rules"
      >
        <a-form-model-item label="文件资源" prop="ids">
          <a-select
            v-model="form.ids"
            mode="multiple"
            style="width: 300px"
            :maxTagCount="1"
          >
            <a-select-option
              v-for="item in fileList"
              :key="item.id"
              :value="item.id"
              >{{ item.name }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
      </a-form-model>
      <template slot="footer">
        <a-button key="back" class="modalCancel" @click="handleCancel"
          >取消</a-button
        >
        <a-button key="submit" @click="primaryClick" type="primary">
          确定
        </a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
import moment from "moment";
export default {
  props: {
    detailData: {},
  },
  data() {
    return {
      fileList: [],
      form: {
        ids: [], //文件资源
      },
      rules: {
        ids: [{ required: true, message: "请选择文件资源", trigger: "blur" }],
      },
      visible: false,
      title: null,
      columns: [
        //主页table列的配置
        {
          title: "序列号",
          dataIndex: "key",
          scopedSlots: { customRender: "key" },
        },
        {
          title: "资源名称",
          dataIndex: "name",
          customRender: (text, record) => {
            return record.name ? record.name : "—";
          },
        },
        {
          title: "资源类型",
          dataIndex: "type",
          customRender: (text, record) => {
            return record.type ? record.type : "—";
          },
        },
        {
          title: "资源地址",
          dataIndex: "url",
          customRender: (text, record) => {
            return record.url ? record.url : "—";
          },
        },
        {
          title: "操作",
          width: 300,
          dataIndex: "operation",
          scopedSlots: { customRender: "operation" },
        },
      ],
      tableData: [
        //主页表格的数据
      ],
      tablePagination: {
        //主页表格的分页器
        total: 0,
        current: 1,
        size: "10",
        showQuickJumper: true, //是否可以快速跳转至某页
        showSizeChanger: true, //是否可以改变 pageSize
      },
    };
  },
  created() {
    this.getData();
    this.getFileList();
  },
  methods: {
    getFileList() {
      this.$axios.get("/sys/resources/page").then((res) => {
        this.fileList = res.response;
        console.log(res.response, 123);
      });
    },
    deleteClick(row) {
      //点击删除
      let that = this;
      this.$confirm({
        title: "确认删除?",
        content: "您确定要删除此项吗?",
        onOk() {
          that.$axios
            .get("/sys/coursePlan/deleteResource",{
              params: {
                id: that.detailData.id,
                resourceId: row.id,
              },
            })
            .then((res) => {
              that.$message.success("删除成功");
              that.getData();
            });
        },
        onCancel() {
          console.log("取消删除");
        },
      });
    },
    editClick(row) {
      //编辑
      for (let i in row) {
        if (i in this.form && i != "time") {
          this.form[i] = row[i];
        }
      }
      this.title = "编辑";
      this.visible = true;
    },
    addbtn() {
      //添加课程计划资源
      this.title = "添加课程计划资源";
      this.visible = true;
    },
    handleCancel() {
      this.form = {
        ids: [], //文件资源
      };
      this.visible = false;
    },
    primaryClick() {
      let resourceIds =
        this.form.ids.length > 0
          ? this.form.ids.filter((item) => item != null).join(",")
          : null;
      this.$nextTick(() => {
        this.$refs.formRef.validate((valid) => {
          if (valid) {
            let data = {
              resourceIds, //课程资源ID(下拉框出来的)
              id: this.detailData.id, //课程计划ID(从父级带过来)
            };
            if (this.title == "添加课程计划资源") {
              this.$axios.put("/sys/coursePlan", data).then((res) => {
                this.$message.success("新增成功");
                this.getData();
                this.handleCancel(); // 关闭模态框
              });
            } else if (this.title == "编辑") {
              this.$axios.put("/sys/coursePlane", data).then((res) => {
                this.$message.success("编辑成功");
                this.getData();
                this.handleCancel(); // 关闭模态框
              });
            }
          } else {
            console.log("校验失败");
          }
        });
      });
    },
    async getData() {
      let data = await this.$axios.get("/sys/resources/getByCourseId", {
        params: { coursePlanId: this.detailData.id },
      });
      data.response.forEach((item, index) => {
        item.key = index + 1;
      });
      this.tableData = data.response;
      this.tablePagination.total = data.response.length;
    },
    backup() {
      this.$emit("changeIsDetail", { status: false });
    },
  },
};
</script>

<style></style>